package com.vpx.server.mapper.system;

import com.vpx.server.bean.entity.SysExceptionLog;
import com.vpx.server.bean.info.system.SysExceptionLogInfo;
import com.vpx.server.bean.order.system.SysExceptionLogPageOrder;
import com.vpx.server.framework.base.BaseMapper;
import com.vpx.server.framework.utils.SqlUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider;

import java.util.List;

/**
 * @author liuks
 * @title: vpx
 * @module: 异常日志
 * @description: 异常日志Mapper
 * @date 2021/09/13 11:42
 */
public interface SysExceptionLogMapper extends BaseMapper<SysExceptionLog> {

    /**
     * 分页查询异常日志
     * @param order
     * @return
     */
    @SelectProvider(type = SysExceptionLogMapper.SqlProvider.class, method = "pageQuery")
    List<SysExceptionLogInfo> pageQuery(@Param("order") SysExceptionLogPageOrder order);

    class SqlProvider{
        public String pageQuery(@Param("order") SysExceptionLogPageOrder order){
            StringBuffer sb = new StringBuffer("select e.*,u.user_name from sys_exception_log e left join sys_user u on e.create_user_id = u.id");
            Boolean where = false;
            if (StringUtils.isNotBlank(order.getExceptionName())) {
                where = SqlUtils.appWhere(where, sb, "e.exception_name like ('%'+#{order.exceptionName}+'%')");
            }
            if (StringUtils.isNotBlank(order.getUserName())) {
                where = SqlUtils.appWhere(where, sb, "u.user_name like ('%'+#{order.userName}+'%')");
            }
            if (StringUtils.isNotBlank(order.getExceptionDateStart())) {
                where = SqlUtils.appWhere(where, sb, " e.create_time >=#{order.exceptionDateStart}");
            }
            if (StringUtils.isNotBlank(order.getExceptionDateEnd())) {
                where = SqlUtils.appWhere(where, sb, " e.create_time<= #{order.exceptionDateEnd}");
            }
//            if (StringUtils.isNotBlank(order.getExceptionDateStart())) {
//                where = SqlUtils.appWhere(where, sb, "#{order.exceptionDateStart} <= date_format(e.create_time,'%Y-%m-%d')");
//            }
//            if (StringUtils.isNotBlank(order.getExceptionDateEnd())) {
//                where = SqlUtils.appWhere(where, sb, "#{order.exceptionDateEnd} >= date_format(e.create_time,'%Y-%m-%d')");
//            }
            sb.append(" order by e.create_time desc");
            return sb.toString();
        }
    }
}
